package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1. 在菜品表中根据name分页查询
        //1-1 准备分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //1-2 准备封装查询条件
        //StringUtils.isNotEmpty(name) 判断name是否为非空字符串
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);

        //1-3 执行菜品列表分页查询
        page = dishMapper.selectPage(page,wrapper);

        //2 遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if(CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //3. 根据菜品的分类id从分类表中查询分类信息(名称)
               Category category= categoryMapper.selectById(dish.getCategoryId());
               dish.setCategoryName(category.getName());
            }
        }
        return page;
    }

    @Override
    public List<Category> findByType(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,type);
        return categoryMapper.selectList(wrapper);
    }

    @Override
    public void save(Dish dish) {
        //1. 向dish表保存基本信息,(mybatisplus自动返回主键)
        dishMapper.insert(dish);
        log.info("保存之后的菜品主键:{}",dish.getId());
        //2. 遍历前端传入的口味列表信息
        List<DishFlavor> flavorList = dish.getFlavors();
        if(CollectionUtil.isNotEmpty(flavorList)){
            for (DishFlavor dishFlavor : flavorList) {
                //3- 为口味设置所属菜品id
                dishFlavor.setDishId(dish.getId());
                //4. 保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(long id) {
        //1.根据id查询菜品信息
        Dish dish = dishMapper.selectById(id);
        //2.根据菜品的分类id从分类表中查询分类信息(名称)
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //3. 根据菜品id从口味表查询当前菜品口味列表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(flavorList);
        return dish;
    }

    @Override
    public void update(Dish dish) {
        //1. 直接更新菜品基本信息
        dishMapper.updateById(dish);

        //2. 删除口味表中当前菜品的相关信息
        //delete from dish_flavor where dish_id = #{}
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //3. 将修改之后口味列表重新保存到口味表中
        List<DishFlavor> flavorList = dish.getFlavors();
        if(CollectionUtil.isNotEmpty(flavorList)){
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(dish.getId());//设置菜品id,否则为null
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public void updateStatus(Integer status, long[] ids) {
        //Dish dish = dishMapper.selectById(id);
        for (long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
       /*Dish dish = new Dish();自己写的
        dish.setStatus(status);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getId,id);
        dishMapper.update(dish,wrapper);*/
    }

    @Override
    public void delete(Long[] ids) {

        //1.判断要删除的商品是否是全部停售
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        int count = dishMapper.selectCount(wrapper);
        if( count > 0){
            throw new CustomException("有在售商品,无法删除");
        }
        //2.遍历删除
        for (long id : ids) {
            //2-1.根据菜品id删除菜品的口味列表
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(wrapper1);
            //2-2.根据id删除菜品
            dishMapper.deleteById(id);
        }
        /*for (long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == 0){
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,dish.getId());
            dishFlavorMapper.delete(wrapper);
            dishMapper.deleteById(id);
            }else {
                throw new CustomException("有在售商品,无法删除");
            }*/
    }

   /* @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        //select * from dish where categoryId = #{categoryId} and status = 1
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,1);//上架
        //菜品集合
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //根据菜品的id从口味表中查询菜品口味信息
                //select * from dish_flavor where dish_id = #{dish.id}
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }*/

    @Override
    public List<Dish> findByCategoryId(String name,Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name)
                .eq(Dish::getStatus,1)
                .eq(categoryId != null,Dish::getCategoryId,categoryId);//上架
        //菜品集合
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //根据菜品的id从口味表中查询菜品口味信息
                //select * from dish_flavor where dish_id = #{dish.id}
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }
}
